<?php

namespace app\logic\single;



use app\api\model\entity\EntityDjEquipmentApiModel;
use app\api\model\entity\EntitySoundLightApiModel;
use app\api\model\entity\EntityStageNameLogoApiModel;
use app\api\model\entity\EntityVoiceApiModel;

class EntitySingle
{
    /**
     * @var EntitySingle
     */
    private static $inc;
    /**
     * @var EntityDjEquipmentApiModel
     */
    private $djEquipmentModel;
    /**
     * @var EntitySoundLightApiModel
     */
    private $soundLightDjModel;
    /**
     * @var EntityStageNameLogoApiModel
     */
    private $stageNameLogoModel;
    /**
     * @var EntityVoiceApiModel
     */
    private $voiceModel;




    private function __clone(){

    }

    public static function getInstance(){
        if(is_null(self::$inc)){
            self::$inc = new self();
        }
        return self::$inc;
    }
    function __construct()
    {
        $this->djEquipmentModel = new EntityDjEquipmentApiModel();
        $this->soundLightDjModel = new EntitySoundLightApiModel();
        $this->stageNameLogoModel = new EntityStageNameLogoApiModel();
        $this->voiceModel = new EntityVoiceApiModel();
    }

    public function getGoodsByType($goods_id,$type){
        [$model,$field] = $this->getModelByType($type);
        if($model === false){
            return false;
        }
        $goods = $model->field($field)->where('id',$goods_id)->find();
        if(empty($goods)){
            return false;
        }
        return $goods;
    }



    public function getGoodsList(array $list)
    {
        foreach ($list as &$val){
            [$model,$field] = $this->getModelByType($val['type']??0);
            if($model === false){
                $val['goods'] = [
                    'title'=>'已删除',
                ];
                continue;
            }
            $val['goods'] = $model->details($val['goods_id'],$field)?:['title'=>'已删除'];
        }
        unset($val);
        return $list;
    }
    function getModelByType(int $type){
        $filed= 'id,title,price,original_price,sale,img';
        switch ($type){
            case 10://Dj单曲
                $model = $this->djEquipmentModel;
                break;
            case 20://声光电
                $model = $this->soundLightDjModel;
                break;
            case 30://艺名logo
                $model = $this->stageNameLogoModel;
                break;
            case 40://人声tag定制
                $model = $this->voiceModel;
                break;
            default:
                return false;
        }
        return [$model,$filed];
    }
}